package map.abstractor.uimodel;


import java.util.ArrayList;
import java.util.List;

import map.tools.interactions.WidgetBehaviorTable;

import org.eclipse.emf.ecore.util.EcoreUtil;

import uimigration.Interactions.Behaviour;
import uimigration.Interactions.InteractionsFactory;
import uimigration.UIModel.Container;
import uimigration.UIModel.Event;
import uimigration.UIModel.Property;
import uimigration.UIModel.UIElement;
import uimigration.UIModel.UIModelFactory;
import uimigration.WidgetsModel.APIModel;
import uimigration.WidgetsModel.Widget;
import uimigration.xmluimodel.Object;


public class AbstractXMLObject {
	
	private static int id=0;
	public AbstractXMLObject(){
	}
	public UIElement process(Object xmlElement, APIModel apimodel,WidgetBehaviorTable table, UIElement parent){
		UIElement element=null;
		Container container;
		Widget widget=apimodel.findWidget(xmlElement.getName());
		String name=xmlElement.getName();
		if(name.contains(":"))
			name=name.substring(name.indexOf(":")+1);
		name=name+""+id;
		if( getXMLProperty(xmlElement, "Name")!=null  )
			name=getXMLProperty(xmlElement, "Name");
		if(widget!=null){
			if(widget.isIsContainer()){
				container= UIModelFactory.eINSTANCE.createContainer();
				//
				container.setId(id++);
				container.setName(name);
				if(parent==null)
				{	
					container.setIsRoot(true);
					parent=container; 
				}
				else
					container.setIsRoot(false);
				try{
					container.getProperties().addAll(getProperties(xmlElement, widget));
				}catch (Exception e) {
					e.printStackTrace();
				}
				container.setWidget ( EcoreUtil.copy(widget));
				List<Event> le=getEvent (xmlElement, table, widget);
				if(le!=null)
					for(Event e : le)
						container.getEvents().add(EcoreUtil.copy(e));
				for(Object o:xmlElement.getChilds()){
					UIElement retour=process(o,apimodel, table, container);
					if(retour!=null)
						container.getContainsUIElement().add(retour);
				}
				//System.out.println(container.getWidgetType());
				return container;
				
			}
			else{
				//
				element=(UIElement) UIModelFactory.eINSTANCE.createUIElement();
				element.setId(id++);
				element.setName(name );
				element.setWidget(EcoreUtil.copy(widget));
				List<Event> le=getEvent (xmlElement, table, widget);
				if(le!=null)
					for(Event e : le)
						element.getEvents().add(EcoreUtil.copy(e));
				try{
					element.getProperties().addAll(getProperties(xmlElement, widget));
				}catch (Exception e) {
					e.printStackTrace();
				}
				return element;
			}
		}
		return null;
		
	}

	public List<Property> getProperties(Object xmlElement, Widget widget)
	{
		List<Property> lp= null;
		lp= new ArrayList<Property>();
		if(widget.getProperties()!=null&&widget.getProperties().size()>0)
		for(uimigration.WidgetsModel.Property p : widget.getProperties())
		{
			
			Property np= UIModelFactory.eINSTANCE.createProperty();
			np.setName(p.getName());
			np.setValue(getXMLProperty(xmlElement, p.getName()));
			np.setValueType(p.getDataType());
			if(p.getRenderingProperty()!=null)
				np.getRenderingProprety().add(InteractionsFactory.eINSTANCE.createDisplayValue());
			lp.add(np);
				
		}
		return lp;

		
	}
	
	private String getXMLProperty(Object xmlElement, String name)
	{
		if(xmlElement!=null && xmlElement.getAttributs()!=null)
			for(uimigration.xmluimodel.Property p:  xmlElement.getAttributs())
			{
				String namep=p.getName();
				if(namep.contains(":")){
					namep= namep.split(":")[1];
					
				}
				if(namep.compareTo(name)==0)
				{	
					return p.getValue();
				
				}
			}
		return null;
	}
	public List<Event> getEvent(Object xmlElement, WidgetBehaviorTable table, Widget widget){
		List<Event> le= new ArrayList<Event>();
		le=new ArrayList<Event>();
		for(uimigration.xmluimodel.Property pi:xmlElement.getAttributs()){
			if(table.isEvent(pi.getName()) || isWidgetEvent(widget, pi.getName())){
				//System.out.println(pi.getName());
				Event e=UIModelFactory.eINSTANCE.createEvent();
				e.setName(pi.getName());
				e.setScriptCode(pi.getValue());
				List<String>lp=table.getLinkedPropertie(xmlElement.getName());
				if(lp!=null)
					for(String s:lp){
						Property prop= UIModelFactory.eINSTANCE.createProperty();
						prop.setName(s);
						e.getLinkedProperty().add(prop);
					}
				List<Behaviour> lb=table.getBehaviors(e.getName());
				if(lb==null || lb.size()==0)
					if(isWidgetEvent(widget, pi.getName()))
						lb=getWidgetEvent(widget, pi.getName()).getEventBehaviourList();
				if(lb!=null)
					for(Behaviour bi : lb)
					{
						e.getImplementedEventBehaviour().add(bi);
					}
				le.add(e);
			}
			
		}
		
		return le;		
	}
	private boolean isWidgetEvent(Widget widget, String name){
		if(widget.getEvents()!=null&&widget.getEvents().size()>0)
		for( uimigration.WidgetsModel.Event e : widget.getEvents())
		{
			if(e.getName().compareTo(name)==0)
				return true;
		}
		return false;
	}

	private uimigration.WidgetsModel.Event getWidgetEvent(Widget widget, String name){
		if(widget.getEvents()!=null&&widget.getEvents().size()>0)
		for( uimigration.WidgetsModel.Event e : widget.getEvents())
		{
			if(e.getName().compareTo(name)==0)
				return e;
		}
		return null;
	}


	public UIElement visit(Object xmlObject, UIElement container){
		return null;

	}

}
